package com.example.module_maintenance

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_base.view.BaseActivity
import com.example.module_maintenance.databinding.ActivityAddVisitorBinding
import com.example.module_maintenance.entity.VisitorEntity
import com.example.module_maintenance.viewmodel.MaintenanceViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@AndroidEntryPoint
@Route(path = RouterPath.PATH_ADD_VISITOR_ACTIVITY)
class AddVisitorActivity : BaseActivity<ActivityAddVisitorBinding>() {

    private val vm by lazy { ViewModelProvider(this)[MaintenanceViewModel::class.java] }

    private val visitorBody: VisitorEntity = VisitorEntity(
        appointmentTime = "",
        approved = true,
        attachments = arrayListOf(),
        employeeId = 0,
        id = 0,
        idNumber = "",
        phoneNumber = "",
        plateNumber = "",
        purpose = "",
        requestParking = false,
        visitStatus = "待审核",
        visitorName = "来访人姓名"


    )
    private var visitData:Long = System.currentTimeMillis()
    private var startTime:Long = 0L
    private var endTime:Long = 1L

    override fun initView() {
        mDataBinding.addVisitorVisitDateInput.setOnClickListener {

            val calendar: Calendar = Calendar.getInstance()
            val year: Int = calendar.get(Calendar.YEAR)
            val month:Int = calendar.get(Calendar.MONTH)
            val day:Int = calendar.get(Calendar.DAY_OF_MONTH)


            DatePickerDialog(this,{
                _,selectedYear,selectedMonth,selectedDay->
              val dateStr = "${selectedYear}/${selectedMonth+1}/${selectedDay}"
                mDataBinding.addVisitorVisitDateInput.text = dateStr

                calendar.set(selectedYear,selectedMonth,selectedDay)
                visitData = calendar.timeInMillis

            },year,month,day).show()

            mDataBinding.addVisitorStartTimeInput.setOnClickListener {
                val calendar:Calendar = Calendar.getInstance()
                val hour:Int = calendar.get(Calendar.HOUR_OF_DAY)
                val minutes:Int = calendar.get(Calendar.MINUTE)
                TimePickerDialog(this,{
                   _,selectedHour,selectedMinutes->

                    val timeStr = "${selectedHour}:${selectedMinutes}"
                    mDataBinding.addVisitorStartTimeInput.text = timeStr

                },hour,minutes,true).show()

                if (endTime<startTime){
                    endTime = startTime+7200000
                }
            }

            mDataBinding.addVisitorEndTimeInput.setOnClickListener {

                val calendar:Calendar = Calendar.getInstance()
                val hour:Int = calendar.get(Calendar.HOUR_OF_DAY)
                val minutes:Int = calendar.get(Calendar.MINUTE)
                TimePickerDialog(this,{
                        _,selectedHour,selectedMinutes->

                    val timeStr = "${selectedHour}:${selectedMinutes}"
                    mDataBinding.addVisitorEndTimeInput.text = timeStr

                },hour,minutes,true).show()
            }

            mDataBinding.addVisitorSave.setOnClickListener {

                if (visitorBody.visitorName.isBlank()){
                    ToastUtils.showLong("请填写完整信息")
                    return@setOnClickListener
                }

                if (mDataBinding.addVisitorParkingSwitch.isChecked){

                }else{

                }


                visitorBody.visitorName = mDataBinding.addVisitorNameInput.text.toString() //来访人姓名
                visitorBody.phoneNumber = mDataBinding.addVisitorPhoneInput.text.toString() //手机号
                visitorBody.idNumber = mDataBinding.addVisitorIdInput.text.toString() //身份证号
                if (mDataBinding.addVisitorPhoneInput.text.toString() == "") { visitorBody.purpose = "其他原因" }
                else { visitorBody.purpose = mDataBinding.addVisitorPurposeInput.text.toString() } //来访事由

                //判断是否进行车位申请
                if(mDataBinding.addVisitorParkingSwitch.isChecked) {
                    visitorBody.plateNumber = mDataBinding.addVisitorPlateNumberInput.text.toString() //车牌号
                    visitorBody.requestParking = true //申请泊车位
                }
                else {
                    visitorBody.plateNumber = ""
                    visitorBody.requestParking = false
                }
                vm.addVisitors(visitorBody)
            }

            vm.addVisitorsSuccess.observe(this){
                ToastUtils.showLong("添加成功")
                finish()
            }
            mDataBinding.addVisitorBack.setOnClickListener {
                onBackPressed()
            }
        }
    }


    override fun getLayoutId(): Int = R.layout.activity_add_visitor


}